import { reqGoodsInfo, reqAddOrShopCar } from '@/api'

const state = {
    goodsInfo: {},

}
const mutations = {
    GETREQGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}
const actions = {
    // detail详情页请求
    async getReqGoodsInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code === 200) {
            commit('GETREQGOODSINFO', result.data)
        }
    },

    async getReqAddOrShopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrShopCar(skuId, skuNum)
        if (result.code === 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('添加购物车失败'))
        }
    }

}
const getters = {

    categoryView(state) {

        return state.goodsInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || []
    }

}
export default {
    state,
    mutations,
    actions,
    getters,
}